/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.app.imageupload;

import java.io.IOException;
import java.util.Properties;

/**
 * A factory for creating ImageUpload objects.
 */
public class ImageUploadFactory {

	private static ImageUploadFactory factory;

	private ImageUploadFactory() {
	}

	/**
	 * Gets the single instance of ImageUploadFactory.
	 *
	 * @return single instance of ImageUploadFactory
	 */
	public static ImageUploadFactory getInstance() {
		if (factory == null) {
			factory = new ImageUploadFactory();
		}
		return factory;
	}

	/**
	 * Gets the image upload method.
	 *
	 * @return the image upload method
	 */
	public ImageUploadMethod getImageUploadMethod() {
		Properties prop = new Properties();
		try {
			prop.load(getClass().getResourceAsStream("/config.properties"));
			String uploadType = prop.getProperty("upload.type");
			ImageUploadMethod method = null;
			if ("qcloud".equals(uploadType)) {
				method = new QCloudImageUploadMethod();
			} else if ("aliyun".equals(uploadType)) {
				method = new AliyunImageUploadMethod();
			} else if ("localanotherserver".equals(uploadType)) {
				method = new LocalAnotherServerImageUploadMethod();
			} else {
				method = new LocalServerImageUploadMethod();
			}
			return method;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
